//
//  FloatingPlayerController.m
//  F FloatingPlayer
//
//  Created by yonas on 2018/9/26.
//  Copyright © 2018年 Inspur. All rights reserved.
//  这里可以维护着window

#import "FloatingViewController.h"
#import "FloatingViewModel.h"
#import "FloatingView.h"
#import "FloatingWindow.h"
@interface FloatingViewController ()
@property(nonatomic,strong) FloatingViewModel *playerModel;

@property(nonatomic,strong) FloatingView *playerView;

@property(nonatomic,strong) FloatingWindow *baseWindow;

@property(nonatomic,weak) id <FloatingPlayerControllerDelegate> delegate;
@end

@implementation FloatingViewController
#pragma mark - public
- (instancetype)initWithPlayerModel:(FloatingViewModel *)playerModel delegate:(id <FloatingPlayerControllerDelegate>) delegate{
    if (self = [super init]){
        _playerModel = playerModel;
        _playerView = [[FloatingView alloc]initWithPlayerModel:_playerModel];
        _delegate = delegate;
        [self _defaultSetting];
    }
    return self;
}

- (void)floatingController_show{
    _baseWindow.hidden = NO;
}
- (void)floatingController_hide{
    _baseWindow.hidden = YES;
    [_baseWindow.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    _baseWindow = nil;
}
- (FloatingWindow *)getFloatingWindow{
    return _baseWindow;
}
#pragma mark - private
- (void)_defaultSetting{
    _baseWindow = [[FloatingWindow alloc]init];
    _baseWindow.windowLevel = UIWindowLevelAlert + 1;
    _baseWindow.backgroundColor = [UIColor clearColor];
    _baseWindow.frame = _playerModel.originalFrame;
    _playerView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    [_baseWindow addSubview:_playerView];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(backToPreController) name:BACK_TO_PRE_CONTROLLER object:nil];
}

- (void)backToPreController{
    [self.delegate floatingController_backToFromVcWithClass:NSClassFromString(self.playerModel.fromVc) floatingModel:self.playerModel floatingViewController:self];
}

- (void)dealloc{
    NSLog(@"%s",__func__);
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
